package com.zc.commons.http.entity;

import com.zc.commons.http.constant.RespMsgConstant;
import com.zc.commons.http.enums.RespCode;
import io.swagger.v3.oas.annotations.media.Schema;

/**
 * @author zhangchunjia
 * @description 响应体
 * @date 2021/12/31
 **/
@Schema(name = "统一响应实体", description = "统一响应实体")
public class Response<T> {

    @Schema(description = "响应码")
    private Integer code;

    @Schema(description = "返回信息")
    private String message;

    @Schema(description = "错误信息")
    private String errorMsg;

    @Schema(description = "数据实体")
    private T data;

    public static Response ok() {
        return new Response(200, RespMsgConstant.SUCCESS, null, null);
    }

    public static <E> Response ok(E data) {
        return new Response(200, RespMsgConstant.SUCCESS, null, data);
    }

    public static <E> Response ok(String message, E data) {
        return new Response(200, message, null, data);
    }

    public static <E> Response error(String errorMsg, E data) {
        return new Response(300, null, errorMsg, data);
    }

    public static <E> Response error(String errorMsg) {
        return new Response(300, null, errorMsg, null);
    }

    public static Response error(RespCode code, String errorMsg) {
        return new Response(code.getCode(), null, errorMsg, null);
    }

    public Response(RespCode respCode, String message, T data) {
        this(respCode, message);
        this.data = data;
    }

    public Response(RespCode respCode, String message) {
        this.code = respCode.getCode();
        if (RespCode.OK.equals(respCode)) {
            this.message = message;
        } else {
            this.errorMsg = message;
        }
    }

    private Response(Integer code, String message, String errorMsg, T data) {
        this.code = code;
        this.message = message;
        this.errorMsg = errorMsg;
        this.data = data;
    }

    public String getMessage() {
        return message;
    }

    public String getErrorMsg() {
        return errorMsg;
    }

    public T getData() {
        return data;
    }

    public Integer getCode() {
        return code;
    }
}
